package longrise.phone.com.bjjt_henan.main;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.bjjt.util.UiUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Adapter.ShareAdapter;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.QRCodeHelper;
import longrise.phone.com.bjjt_henan.Utils.RecycleBitmap;

/**
 * 推荐好友界面
 */
public class RecommendView extends LFView {

    private static final String LOAD = "http://220.231.252.128/hnkckp_webmap/hnkckp_ConTraDownLoad";

    private Context context;

    private View view;

    private ImageView imageView;

    private GridView gridView;

    private QRCodeHelper helper;

    private List<EntityBean> shareLists;

    public RecommendView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        if (null == context) {
            return;
        }
        view = LayoutInflater.from(context).inflate(R.layout.view_recommend, null);
        if (null == view) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("推荐好友");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeForm();
                OnDestroy();
            }
        });
        imageView = (ImageView) view.findViewById(R.id.recommend_iv_code);
        gridView = (GridView) view.findViewById(R.id.recommend_gv);

        showImage(LOAD);
        showShareView();
    }

    //显示二维码图片
    private void showImage(String str) {
        try {
//            byte[] bys = str.getBytes("utf-8");//utf-8编码
//            if (null != bys && 0 < bys.length) {
//                for (int i = 0; i < bys.length; i++) {
//                    bys[i] = (byte) (bys[i] ^ 1);//异或加密
//                }
//                str = new String(bys, "ISO-8859-1");//ISO-8859-1编码
//            }
            if (null == helper) {
                helper = new QRCodeHelper();
            }
            imageView.setImageBitmap(helper.createQRImage(str, 250));//参数1：二维码字串；参数2：二维码尺寸
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            helper = null;
        }
    }

    //显示分享列表
    private void showShareView() {
        getShareData();
        ShareAdapter adapter = new ShareAdapter(context);
        gridView.setAdapter(adapter);
        adapter.setDataLists(shareLists);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EntityBean bean = shareLists.get(position);
                switch (bean.getInt("id")) {
                    case 0://新浪微博
                        shareTo(SHARE_MEDIA.SINA);
                        break;
                    case 1://微信朋友圈
                        shareTo(SHARE_MEDIA.WEIXIN_CIRCLE);
                        break;
                    case 2://微信
                        shareTo(SHARE_MEDIA.WEIXIN);
                        break;
                    case 3://QQ空间
                        shareTo(SHARE_MEDIA.QZONE);
                        break;
                    case 4://QQ好友
                        shareTo(SHARE_MEDIA.QQ);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    //分享到其他应用
    private void shareTo(SHARE_MEDIA var1) {
        ShareAction action = new ShareAction((Activity) context);
        action.setPlatform(var1);
        action.withTitle("交通事故快处快赔");//标题在微博中无用，可不用写
        action.withText("轻微交通事故手机处理，交警定责、保险报案、定责理赔全程最快5分钟！河南交警、保监会联合出品");
        action.withTargetUrl(LOAD);//点击分享内容打开的链接
        action.withMedia(new UMImage(context, R.mipmap.logo09));//附带的图片，音乐，视频等多媒体对象
        action.setCallback(umShareListener);//设置友盟分享后状态监听
        action.share();//发起分享，调起微信，QQ，微博客户端进行分享
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                UiUtil.showToast(context, platform + " 收藏成功啦");
            } else {
                UiUtil.showToast(context, platform + " 分享成功啦");
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            UiUtil.showToast(context, platform + " 分享失败啦");
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            UiUtil.showToast(context, platform + " 分享取消啦");
        }
    };

    @Override
    public void refreshByTime() {

    }

    @Override
    public void refresh() {

    }

    //获取分享的类型
    private void getShareData() {
        if (null == shareLists) {
            shareLists = new ArrayList<EntityBean>();
        } else {
            shareLists.clear();
        }
//        EntityBean sinaBean = new EntityBean();
//        sinaBean.set("id", 0);
//        sinaBean.set("text", "新浪微博");
//        sinaBean.set("resid", R.mipmap.ico01);
//        shareLists.add(sinaBean);
//        sinaBean = null;

        EntityBean wxBean = new EntityBean();
        wxBean.set("id", 2);
        wxBean.set("text", "微信");
        wxBean.set("resid", R.mipmap.ico03);
        shareLists.add(wxBean);
        wxBean = null;

        EntityBean wxfBean = new EntityBean();
        wxfBean.set("id", 1);
        wxfBean.set("text", "微信朋友圈");
        wxfBean.set("resid", R.mipmap.ico02);
        shareLists.add(wxfBean);
        wxfBean = null;

        EntityBean qqBean = new EntityBean();
        qqBean.set("id", 4);
        qqBean.set("text", "QQ好友");
        qqBean.set("resid", R.mipmap.ico05);
        shareLists.add(qqBean);
        qqBean = null;

        EntityBean qqsBean = new EntityBean();
        qqsBean.set("id", 3);
        qqsBean.set("text", "QQ空间");
        qqsBean.set("resid", R.mipmap.ico04);
        shareLists.add(qqsBean);
        qqsBean = null;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        RecycleBitmap.recycleImageView(imageView);
        if (null != shareLists) {
            shareLists.clear();
            shareLists = null;
        }
        if (null != gridView) {
            gridView.setAdapter(null);
            gridView.setOnItemClickListener(null);
            gridView = null;
        }
        view = null;
        umShareListener = null;
    }
}
